package com.wlj.util;



import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtTokenUtil {
    // Token请求头
    public static final String TOKEN_HEADER = "Authorization";
    // Token前缀
    public static final String TOKEN_PREFIX = "Bearer ";

    // 签名主题
    public static final String SUBJECT = "piconjo";
    // 过期时间
    public static final long EXPIRITION = 1000 * 24 * 60 * 60 * 7;
    //public static final long EXPIRITION = 1000 * 60;
    // 应用密钥
    public static final String APPSECRET_KEY = "Yik8MJeqpcO97HX1+WM1o38pG6ln0otV0O/WS6eVcF/uQNLUX9IJY6lRMnJh 9jdJjlBAmfhMPZ5GOyuji39RwWU7OQse/iEfY5fgE/eTZPCWqJpDykqtVYJ+ ZWV1Y6Tk+U8EilEuDCw4uAmYYGvH3oaM0scWzat9TJvvdFRt1E8=";
    // 角色权限声明
    private static final String ROLE_CLAIMS = "role";
    private static final String ISS = "paperpass";

//    @Autowired
//    private static RedisUtils redisUtils;

    /**
     * 生成Token
     */
    public static String createToken(String username, String role) {
//        Map<String, Object> map = new HashMap<>();
//        map.put(ROLE_CLAIMS, "ROLE_" + role);
//        String token = Jwts
//                .builder()
//                .setSubject(username)
//                .setClaims(map)
//                .claim("username", username)
//                .setIssuer(ISS)
//                .setIssuedAt(new Date())
//                .setExpiration(new Date(System.currentTimeMillis() + EXPIRITION))
//                .signWith(SignatureAlgorithm.HS256, APPSECRET_KEY)
//                .compact();

        //两个都可以用
        HashMap<String, Object> map = new HashMap<>();
        map.put(ROLE_CLAIMS,role);
        String token=Jwts.builder()
                .setSubject(username)
                .setClaims(map)
                .claim("username",username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis()+EXPIRITION))
                .signWith(SignatureAlgorithm.HS512,APPSECRET_KEY)
                .compact();


        System.out.println("token失效时间：" + new Date(System.currentTimeMillis() + EXPIRITION));
        return token;
    }

    /**
     * 校验Token
     */
    public static Claims checkJWT(String token) {
        try {
            return Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 从Token中获取username
     */
    public static String getUsername(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        return claims.get("username").toString();
    }

    /**
     * 从Token中获取用户角色
     */
    public static String getUserRole(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        return claims.get("role").toString();
    }

    /**
     * 校验Token是否过期
     */
    public static boolean isExpiration(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        return claims.getExpiration().before(new Date());
    }

    public static void refreshTokenExpiration(String token) {
        Claims claims = Jwts.parser().setSigningKey(APPSECRET_KEY).parseClaimsJws(token).getBody();
        claims.setExpiration(new Date(System.currentTimeMillis() + EXPIRITION));
        System.out.println("token失效时间：" + new Date(System.currentTimeMillis() + EXPIRITION));
    }
}
